package com.sunchangpeng.wechat.mp.menu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.sunchangpeng.wechat.core.WechatClient;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import com.sunchangpeng.wechat.mp.menu.bean.Menu;
import com.sunchangpeng.wechat.mp.menu.bean.RuleMenu;
import lombok.Data;

import java.util.List;

public class Menus {
    protected WechatClient wechatClient;

    public void setWechatClient(WechatClient wechatClient) {
        this.wechatClient = wechatClient;
    }

    public static Menus with(MpAppSetting appSetting) {
        Menus menus = new Menus();
        menus.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return menus;
    }

    /**
     * 自定义菜单创建
     */
    public void createMenu(Menu menu) {
        String url = EndpointProvider.get("url.menu.create");
        wechatClient.getTextWithToken(url, JSON.toJSONString(menu));
    }

    /**
     * 删除所有菜单
     */
    public void delete() {
        String url = EndpointProvider.get("url.menu.delete");
        wechatClient.getTextWithToken(url);
    }

    /**
     * 菜单查询
     */
    public MenuWrapper get() {
        String url = EndpointProvider.get("url.menu.get");
        String content = wechatClient.getTextWithToken(url);
        return JSON.parseObject(content, MenuWrapper.class);
    }

    /**
     * 获取自定义菜单配置接口
     */
    public void getMenuConfig() {
        throw new RuntimeException("暂不支持该接口");
    }

    @Data
    public static class MenuWrapper {
        private Menu menu;

        @JSONField(name = "conditionalmenu")
        private List<RuleMenu> ruleMenus;
    }
}
